package cn.stylefeng.guns.modular.rw.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.core.ParamUtil;
import cn.stylefeng.guns.modular.rw.entity.Carousel;
import cn.stylefeng.guns.modular.rw.service.ICarouselService;
import cn.stylefeng.guns.modular.rw.service.IImagesService;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 描述：轮播表 controller
 * 作者：chenhuiyan
 * 时间：2020-04-31
 */
@Controller
@RequestMapping("/rw/carousel")
public class CarouselController extends BaseController {

    private String PREFIX = "/modular/rw/carousel/";

    @Autowired
    private ICarouselService carouselService;

    /**
     * 跳转到管理首页
     */
    @RequestMapping(value = "")
    public String index() {
        return PREFIX + "carousel.html";
    }

    /**
     * 获取列表
     */
    @ResponseBody
    @RequestMapping(value = "/list")
    public Object list() {
        Map<String, Object> params = ParamUtil.getParams();
        Page<Map<String, Object>> list = carouselService.list(params);
        return LayuiPageFactory.createPageInfo(list);
    }

    /**
     * 跳转添加/修改页面
     * @param model
     * @param carouselId
     * @return
     */
    @RequestMapping(value = "/addOrEdit")
    public String addOrEdit(Model model, String carouselId) {
        model.addAttribute("carouselId", carouselId);
        return PREFIX + "carousel_add.html";
    }

    /**
     * 保存
     * @param carousel
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save")
    public ResponseData save(Carousel carousel) {
        if (ToolUtil.isEmpty(carousel.getId())) {
            carousel.preInsert();
            carouselService.save(carousel);
        }else{
            carousel.preUpdate();
            carouselService.updateById(carousel);
        }
        return SUCCESS_TIP;
    }

    /**
     * 获取详情
     * @param carouselId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getInfoById/{carouselId}")
    public Object getInfoById(@PathVariable("carouselId") String carouselId){
        Map<String, Object> res = carouselService.getInfoById(carouselId);
        return res;
    }

    /**
     * 删除图片（物理删除）
     * @param carouselId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete")
    public ResponseData delete(@RequestParam(name = "carouselId") String carouselId){
        if(ToolUtil.isEmpty(carouselId)){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        carouselService.removeById(carouselId);
        return SUCCESS_TIP;
    }

}
